package com.xxxx.worm;


import com.xxxx.mysql.model.WormLink;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class HtmlParse {

    /**
     * 获取页面所有类型的此标签
     *
     * @param page        抓取到的对象
     * @param cssSelector 获取标签
     * @return
     */
    public static Elements select(WormObj page, String cssSelector) {
        return page.getDocument().select(cssSelector);
    }

    /**
     * 获取具体的标签
     *
     * @param page        抓取到的对象
     * @param cssSelector 获取标签
     * @param index       第几个
     * @return
     */
    public static Element select(WormObj page, String cssSelector, int index) {
        Elements elements = select(page, cssSelector);
        int realIndex = index;
        if (index < 0) {
            realIndex = elements.size() + index;
        }
        return elements.get(realIndex);
    }

    /**
     * 获取指定选择器中的链接
     * @param page 抓取到的对象
     * @param cssSelector 选择器
     * @return
     */
    public static List<WormLink> getLinks(WormObj page, String cssSelector) {
        Set<String> links = new HashSet<String>();
        Elements cssSelectors = select(page, cssSelector);
        Iterator<Element> eles = cssSelectors.iterator();
        List<WormLink> wls = new ArrayList<WormLink>();
        while (eles.hasNext()){
            WormLink wormLink = new WormLink();
            Element next = eles.next();
            if (next.hasAttr("href")) {
                 Pattern p = Pattern.compile("<a.*?>(.+?)</a>");
                String nextString = next.toString();
                Matcher matcher = p.matcher(nextString);
                while (matcher.find()){
                    String attr = matcher.group(1);
                    wormLink.setLinkName(attr);
                    wormLink.setLinkUrl(next.attr("abs:href"));
                    wormLink.setUpperLevel(cssSelector);
                    wormLink.setCreateTime(new Date());
                    wormLink.setContent(page.getUrl());
                }

            } else if (next.hasAttr("src")) {
                links.add(next.attr("abs:src"));
            }
            wls.add(wormLink);
        }
        return wls;
    }

    /**
     * 获取指定选择器中的属性值
     * ex：获取("img[src]","abs:src")的值
     * @param page 抓取到的对象
     * @param cssSelector 选择器
     * @param attrName 属性值
     * @return
     */
    public static ArrayList<String> getAttrs(WormObj page, String cssSelector, String attrName) {
        ArrayList<String> list = new ArrayList<String>();
        Elements selects = select(page, cssSelector);
        for (Element select : selects) {
            if (select.hasAttr(attrName)) {
                list.add(select.attr(attrName));
            }
        }
        return list;
    }
}
